<template>
  <el-card class="categoty-card">
    <el-form label-width="120px" :inline="true" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择一级分类"
          :modelValue="categoryStore.categoty1Id"
          @update:modelValue="category1IdChange"
        >
          <el-option
            v-for="c1 in categoryStore.categoty1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择二级分类"
          :modelValue="categoryStore.categoty2Id"
          @update:modelValue="category2IdChange"
        >
          <el-option
            v-for="c2 in categoryStore.categoty2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择三级分类"
          v-model="categoryStore.categoty3Id"
        >
          <el-option
            v-for="c3 in categoryStore.categoty3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
};
</script>

<script lang="ts" setup>
import { onMounted, onBeforeMount } from "vue";
import { userCategoryStore } from "@/stores/category";

const categoryStore = userCategoryStore();

defineProps(["disabled"]);
// 一级数据
onMounted(() => {
  categoryStore.category1List();
});
// 二级数据
// newVal当下拉框值发生变化时会触发，v-model的 update:modelValue
const category1IdChange = (newVal: number) => {
  categoryStore.category2List(newVal);
};

// 三据数据
const category2IdChange = async (newVal2: number) => {
  categoryStore.category3List(newVal2);
};
onBeforeMount(() => {
  categoryStore.categoty1Id = undefined;
  categoryStore.categoty2Id = undefined;
  categoryStore.categoty3Id = undefined;
  categoryStore.categoty1List = [];
  categoryStore.categoty2List = [];
  categoryStore.categoty3List = [];
});
</script>

<style scoped></style>
<style>
.categoty-card .el-form-item {
  margin: 10px 0;
}
</style>